;
(function () {

  'use strict';

  angular.module('services.customActivity.summary')
    .service('summaryUpaySuccessRateService', summaryUpaySuccessRateService)
    .service('summaryProvinceBusSuccessRateService', summaryProvinceBusSuccessRateService)
    .service('summaryProvinceSysSuccessRateService', summaryProvinceSysSuccessRateService)
    .service('summaryTotalBusSuccessRateService', summaryTotalBusSuccessRateService)
    .service('summaryTotalSysSuccessRateService', summaryTotalSysSuccessRateService)
    .service('summaryProvinceTotalBusSuccessRateService', summaryProvinceTotalBusSuccessRateService);

  function summaryProvinceTotalBusSuccessRateService(Restangular, generatorSummaryPageService, COREDATA) {

    var that = this;
    this.max = [];
    this.min = [];

    var option = {
      title: {
        show: false
      },
      tooltip: {
        trigger: 'item',
        formatter: function (params) {
          if (params[2] === '-') {
            return '暂不统计';
          }
          params[2] = parseFloat(params[2]).toFixed(2);
          return params[1] + '<br/>' + params[2] + '%';
        }
      },
      legend: {
        show: false,
        data: []
      },
      toolbox: {
        show: false
      },
      series: [
        {
          name: '',
          type: 'map',
          mapType: 'china',
          itemStyle: {
            normal: {color: '#eee', borderColor: '#fff', borderWidth: '.7', label: {show: true}}
          },
          clickable: true,
          mapLocation: {y: 'top', height: 290},
          data: []
        },
        {
          name: '',
          type: 'map',
          mapType: 'china',
          clickable: true,
          itemStyle: {
            normal: {color: '#2196f3', borderColor: '#fff', borderWidth: '.7', label: {show: true}}
          },
          mapLocation: {y: 'top', height: 290},
          data: []
        }
      ]
    };

    this.init = function (element) {

      if (generatorSummaryPageService.pageInfo.preview) {
        setTimeout(function () {
          that.loading = true;
          that.max = [null, null, null];
          that.min = [null, null, null];
          option.series[1].data = [];
          angular.forEach(generatorSummaryPageService.pageInfo.provinceCodes, function (code) {
            angular.forEach(COREDATA.PROVINCES, function (province) {
              if (code === province.provinceCode) {
                option.series[1].data.push({
                  value: 0.00,
                  name: province.provinceName
                });
              }
            });
          });
          (echarts.init(element)).setOption(option);
        }, 0);
        return;
      }

      that.loading = false;
      Restangular.all('sas/customActivity/summary/successRate/proBusProcessBus/query').post({
        activityId: generatorSummaryPageService.pageInfo.activityId
      }).then(function (result) {
        that.loading = true;
        if (result.state === '10000') {
          var list = [];
          that.max = [null, null, null];
          that.min = [null, null, null];
          //降序排序 先按成功率 再按业务量 再按省CODE
          result.data.sort(function (a, b) {
            a.provinceCode = parseFloat(a.provinceCode);
            b.provinceCode = parseFloat(b.provinceCode);
            if (a.rate > b.rate) {
              return -1;
            } else if (a.rate === b.rate) {
              if (a.totalVolume > b.totalVolume) {
                return -1;
              } else if (a.totalVolume === b.totalVolume) {
                if (a.provinceCode > b.provinceCode) {
                  return -1;
                } else if (a.provinceCode === b.provinceCode) {
                  return 0;
                } else {
                  return 1;
                }
              } else {
                return 1;
              }
            } else {
              return 1;
            }
          });
          angular.forEach(result.data, function (obj) {
            list.push({
              value: parseFloat(obj.rate),
              name: obj.provinceName,
              volume: obj.totalVolume
            });
          });
          option.series[1].data = list;
          //过滤掉业务量为零的数据
          var temp = list.filter(function (obj) {
            return obj.volume > 0;
          });
          //一前一后 取出前三后三
          for (var i = 0, j = Math.ceil(temp.length / 2); i < j && i < 3; i++) {
            that.max[i] = temp[i];
            if (i !== temp.length - 1 - i) {
              that.min[i] = temp[temp.length - 1 - i];
            }
          }
          (echarts.init(element)).setOption(option);
        }
      });
    };
  }

  summaryProvinceTotalBusSuccessRateService.$inject = ['Restangular', 'generatorSummaryPageService', 'COREDATA'];

  function summaryTotalBusSuccessRateService(Restangular, generatorSummaryPageService) {

    this.UPAY = 0;
    this.TMALL = 0;
    this.SCPAY = 0;
    this.BANK = 0;
    var that = this;

    this.queryBySys = function (sys) {

      if (generatorSummaryPageService.pageInfo.preview) {
        setTimeout(function () {
          that.loading = true;
          that[sys] = '0.00%';
          that[sys + 'Color'] = '#2196F3';
        }, 0);
        return;
      }

      that.loading = false;
      Restangular.all('sas/customActivity/summary/successRate/busProcessBus/query').post({
        activityId: generatorSummaryPageService.pageInfo.activityId
      }).then(function (result) {
        that.loading = true;
        if (result.state === '10000') {
          that[sys] = result.data[sys.toLowerCase()].rate;
          that[sys + 'Color'] = '#FFF';
          if (that[sys] < 97) {
            that[sys + 'Color'] = '#333';
          }
          that[sys] += '%';
        } else {
          that[sys] = '0.00%';
          that[sys + 'Color'] = '#333';
        }
      });
    };
  }

  summaryTotalBusSuccessRateService.$inject = ['Restangular', 'generatorSummaryPageService'];

  function summaryTotalSysSuccessRateService(Restangular, generatorSummaryPageService) {

    var that = this;

    this.queryBySys = function (sys) {

      if (generatorSummaryPageService.pageInfo.preview) {
        setTimeout(function () {
          that.loading = true;
          that[sys] = '0.00%';
          that[sys + 'Color'] = '#2196F3';
        }, 0);
        return;
      }

      that.loading = false;
      Restangular.all('sas/customActivity/summary/successRate/busProcessSys/query').post({
        activityId: generatorSummaryPageService.pageInfo.activityId
      }).then(function (result) {
        that.loading = true;
        if (result.state === '10000') {
          that[sys] = result.data[sys.toLowerCase()].rate;
          that[sys + 'Color'] = '#FFF';
          if (that[sys] < 97) {
            that[sys + 'Color'] = '#333';
          }
          that[sys] += '%';
        } else {
          that[sys] = '0.00%';
          that[sys + 'Color'] = '#333';
        }
      });
    };
  }

  summaryTotalSysSuccessRateService.$inject = ['Restangular', 'generatorSummaryPageService'];

  function summaryProvinceBusSuccessRateService(Restangular, generatorSummaryPageService) {

    var that = this;

    this.queryBySys = function (sys) {

      if (generatorSummaryPageService.pageInfo.preview) {
        setTimeout(function () {
          that.loading = true;
          that[sys] = '0.00%';
          that[sys + 'Color'] = '#2196F3';
        }, 0);
        return;
      }

      that.loading = false;
      Restangular.all('sas/customActivity/summary/successRate/provinceBus/query').post({
        activityId: generatorSummaryPageService.pageInfo.activityId
      }).then(function (result) {
        that.loading = true;
        if (result.state === '10000') {
          that[sys] = result.data[sys.toLowerCase()].rate;
          that[sys + 'Color'] = '#FFF';
          if (that[sys] < 97) {
            that[sys + 'Color'] = '#333';
          }
          that[sys] += '%';
        } else {
          that[sys] = '0.00%';
          that[sys + 'Color'] = '#333';
        }
      });
    };
  }

  summaryProvinceBusSuccessRateService.$inject = ['Restangular', 'generatorSummaryPageService'];

  function summaryProvinceSysSuccessRateService(Restangular, generatorSummaryPageService) {

    var that = this;

    this.queryBySys = function (sys) {

      if (generatorSummaryPageService.pageInfo.preview) {
        setTimeout(function () {
          that.loading = true;
          that[sys] = '0.00%';
          that[sys + 'Color'] = '#2196F3';
        }, 0);
        return;
      }

      that.loading = false;
      Restangular.all('sas/customActivity/summary/successRate/provinceSys/query').post({
        activityId: generatorSummaryPageService.pageInfo.activityId
      }).then(function (result) {
        that.loading = true;
        if (result.state === '10000') {
          that[sys] = result.data[sys.toLowerCase()].rate;
          that[sys + 'Color'] = '#FFF';
          if (that[sys] < 97) {
            that[sys + 'Color'] = '#333';
          }
          that[sys] += '%';
        } else {
          that[sys] = '0.00%';
          that[sys + 'Color'] = '#333';
        }
      });
    };
  }

  summaryProvinceSysSuccessRateService.$inject = ['Restangular', 'generatorSummaryPageService'];

  function summaryUpaySuccessRateService(Restangular, generatorSummaryPageService) {

    var that = this;

    this.init = function (type) {

      if (generatorSummaryPageService.pageInfo.preview) {
        setTimeout(function () {
          that.loading = true;
          that[type] = '0.00%';
          that[type + 'Color'] = '#2196F3';
        }, 0);
        return;
      }

      that.loading = false;
      Restangular.all('sas/customActivity/summary/successRate/upay/query').post({
        activityId: generatorSummaryPageService.pageInfo.activityId
      }).then(function (result) {
        that.loading = true;
        if (result.state === '10000') {
          that[type] = result.data[type.toLowerCase() + 'SuccessRate'].rate;
          that[type + 'Color'] = '#FFF';
          if (that[type] < 97) {
            that[type + 'Color'] = '#333';
          }
          that[type] += '%';
        } else {
          that[type] = '0.00%';
          that[type + 'Color'] = '#333';
        }
      });
    };
  }

  summaryUpaySuccessRateService.$inject = ['Restangular', 'generatorSummaryPageService'];
})();